'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 神庙
 * @author xiaoxu
 */

var _ = require('lodash');
var moment = require('moment');
var mongoose = require('mongoose');
var autoIncrement = require('mongoose-auto-increment');
var dataApi = require('../libs/dataApi');
var constantUtil = require('../util/constantUtil');
var envUtil = require('../util/envUtil');
var mgrUtil = require('../util/mgrUtil');
var eventType = require('../consts/eventType.js');

// 初始每日免费次数
var _initFreeNum = function () {
    return constantUtil.value('wishingtree_free_time');
};
/**
 * 神庙
 */
var wishtreeSchema = new mongoose.Schema({
    // id
    role_id: {
        type: Number,
        required: true,
        index: true
    },

    // 资源已购买神庙列表
    wishtree_buy_num_list: {
        type: mongoose.Schema.Types.Mixed,
        default: {}
    },

    // // 折扣商城次数
    // discout_shop_num_list: {
    //     type: mongoose.Schema.Types.Mixed,
    //     default: {}
    // },

    // 已领取免费次数
    free_wishtree_num: {
        type: Number,
        default: _initFreeNum
    },

    // VIP免费次数
    vip_free_wishtree_num: {
        type: Number,
        default: 0
    },

    // 刷新时间
    last_refresh_time: {
        type: Date,
        default: 0
    }
});

/**
 * 增加免费次许愿树免费次数
 * @return {[type]} [description]
 */
wishtreeSchema.methods.addFreeNum = function (num) {
    this.free_wishtree_num += num;
};

/**
 * 获取vip免费次数
 * @return {[type]} [description]
 */
wishtreeSchema.methods.getVipNum = function () {
    return this.vip_free_wishtree_num;
};

/**
 * 设置vip免费次数
 * @return {[type]} [description]
 */
wishtreeSchema.methods.setVipNum = function (num) {
    this.vip_free_wishtree_num = (+num);
};

/**
 * 获取许愿树等级
 * @return {[type]} [description]
 */
wishtreeSchema.methods.getWishtreeLv = function () {
    return this.wishtree_lv;
};

/**
 * 升级许愿树
 * @return {[type]} [description]
 */
wishtreeSchema.methods.addWishtreeLv = function () {
    this.wishtree_lv++;
};

/**
 * 扣除免费次数
 * @return {[type]} [description]
 */
wishtreeSchema.methods.subFreeWishtreeNum = function () {
    if (this.free_wishtree_num <= 0) {
        this.free_wishtree_num = 0;
    } else {
        this.free_wishtree_num--;
    }

};

/**
 * 获取免费次数
 * @return {[type]} [description]
 */
wishtreeSchema.methods.getFreeWishtreeNum = function () {
    return this.free_wishtree_num;
};

/**
 * 增加已购买type
 * @return {[type]} [description]
 */
wishtreeSchema.methods.addWishtreeNum = function (Wishtree_type) {
    this.wishtree_buy_num_list[Wishtree_type] = this.wishtree_buy_num_list[Wishtree_type] || 0;
    this.wishtree_buy_num_list[Wishtree_type]++;
    this.markModified('wishtree_buy_num_list');
};

/**
 * 获取已购买type
 * @return {[type]} [description]
 */
wishtreeSchema.methods.getWishtreeList = function () {
    return this.wishtree_buy_num_list;
};

/**
 * 获取刷新时间
 * @return {[type]} [description]
 */
wishtreeSchema.methods.getRefreshTime = function () {
    return this.last_refresh_time;
};

/**
 * 获取已购买type
 * @return {[type]} [description]
 */
wishtreeSchema.methods.getWishtreeTotal = function () {
    var num = 0;
    _.each(this.wishtree_buy_num_list, function (value) {
        num += (+value);
    });
    return num;
};

/**
 * 获取已购买type 次数
 * @return {[type]} [description]
 */
wishtreeSchema.methods.getWishtreeNum = function (Wishtree_type) {
    return this.wishtree_buy_num_list[Wishtree_type] || 0;
};

/**
 * 刷新
 * @return {[type]} [description]
 */
wishtreeSchema.methods.refresh = function () {
    this.wishtree_buy_num_list = {};
    this.discount_shop_num_list = {};
    this.free_wishtree_num = _initFreeNum() + this.getVipNum();
    this.last_refresh_time = Date.now();
    this.discout_shop_num_list = {};
    this.markModified('discout_shop_num_list');
};

wishtreeSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 获取修改
 * @return {[type]} [description]
 */
wishtreeSchema.methods.getLastChange = function () {
    // var change_info = this.toJSON();
    // delete change_info.role_id;
    // delete change_info.wishtree_buy_num_list;
    // delete change_info.vip_free_wishtree_num;
    // delete change_info.last_refresh_time;
    // return change_info;

    var ret = {};
    ret.free_wishtree_num = this.free_wishtree_num;
    return ret;
};

/**
 * 推送给用户
 * @return {[type]} [description]
 */
wishtreeSchema.methods.push = function () {
    this.pushModify();
};

wishtreeSchema.methods.pushModify = function () {
    var self = this;
    mgrUtil.sendStatusMessage(
        [self.role_id],
        eventType.ON_WISHTREE_CHANGE,
        self.getLastChange(),
        function (err, fails) {
            if (err) {
                console.error('send arena message error: %j, fail ids: %j', err, fails);
                return;
            }
        }
    );
};

if (!wishtreeSchema.options.toJSON) {
    wishtreeSchema.options.toJSON = {};
}
/* jshint unused:false */
wishtreeSchema.options.toJSON.transform = function (doc, ret) {
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Wishtree', wishtreeSchema);
